package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.ReservationCountByTimeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 预约信息Service业务层处理
 * 
 * @author zhx
 * @date 2025-05-23
 */
@Service
@Slf4j
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String RESERVATION_COUNT = "reservationcount";

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @Override
    public Integer getCancelledReservationCount() {
        //获取ThreadLocal中的用户id
        Long userId = UserThreadLocal.getUserId();
        //从缓存中查找
        Object object = redisTemplate.opsForHash().get(RESERVATION_COUNT, "count" + userId);
        if (object != null){
            return (Integer) object;
        }

        //如果缓存中没有从数据库中查,再添加到缓存
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        if (object == null){
            Reservation reservation = this.lambdaQuery().eq(Reservation::getStatus, 2)
                    .eq(Reservation::getId, userId)
                    .between(Reservation::getUpdateTime, startTime, endTime)
                    .one();
            if (reservation == null){
                throw new BaseException("未查询到预约信息");
            }
        }
        redisTemplate.opsForHash().put(RESERVATION_COUNT, "count" + userId,1);

        return 1;
    }

    /**
     * 根据时间段查询预约数量
     *
     * @param time
     * @return
     */
    @Override
    public List<ReservationCountByTimeVo> getCountByTime(Long time) {

        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        return reservationMapper.getCountByTime(startTime,endTime);
    }

    /**
     * 取消预约信息
     *
     * @param id
     * @return
     */
    @Override
    public int cancelReservation(Integer id) {
        LambdaUpdateChainWrapper<Reservation> set = this.lambdaUpdate().eq(Reservation::getId, id)
                .set(Reservation::getStatus, 2);
        if (set == null){
            throw new BaseException("取消预约失败");
        }
        return 1;
    }
}
